package com.company;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelDemo {
  public static void main(String[] args) throws Exception {
    File file=new File("C:\\Users\\张小飞\\Desktop\\888.sql");
    FileInputStream fileInputStream=new FileInputStream(file);
    FileChannel fileChannel=fileInputStream.getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(20);
    ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
    int count=0;
    while ((count=fileChannel.read(buffer))!=-1){
      buffer.flip();
      while (buffer.hasRemaining()){
        byteArrayOutputStream.write(buffer.get());
      }
      buffer.clear();
    }
    System.out.println(new String(byteArrayOutputStream.toByteArray()));
    fileChannel.close();
    fileInputStream.close();
  }
}
